package com.wpl.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wpl.R;
import com.wpl.user.entity.Music;
import com.wpl.user.entity.User;
import com.wpl.user.entity.vo.MusicQueryVo;
import com.wpl.user.entity.vo.UserQueryVo;
import com.wpl.user.service.MusicService;
import com.wpl.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/wpl/music")
@Api(description = "音乐管理")
public class MusicController {
    @Autowired
    private MusicService musicService;
    @GetMapping("findAll")
    @ApiOperation("所有音乐")
    public R findAll(){
        List<Music> musics = musicService.list(null);
        System.out.println("-------------");
        return R.ok().data("items",musics);
    }

    @DeleteMapping("{id}")
    @ApiOperation("逻辑删除音乐")
    public R deleteById(@ApiParam(name="id",value="音乐id",required=true) @PathVariable Integer id){
        boolean flag = musicService.removeById(id);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping("pageFind/{current}/{limit}")
    @ApiOperation("分页查询")
    public R pageFind(@ApiParam(name="current",value="当前页",required=true) @PathVariable Integer current,@ApiParam(name="limit",value="当前页大小",required=true) @PathVariable Integer limit){
        Page page = new Page(current,limit);
        musicService.page(page,null);
        Long total = page.getTotal();
        List<User> list = page.getRecords();
        return R.ok().data("total",total).data("items",list);
    }

    @PostMapping("pageCondition/{current}/{limit}")
    @ApiOperation("分页带条件查询")
    public R pageCondition(@ApiParam(name="current",value="当前页",required=true) @PathVariable Integer current, @ApiParam(name="limit",value="当前页大小",required=true) @PathVariable Integer limit, @RequestBody(required = false) MusicQueryVo musicQueryVo){
        Page page = new Page(current,limit);
        QueryWrapper queryWrapper = new QueryWrapper();
        String name = musicQueryVo.getName();
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        musicService.page(page,queryWrapper);
        Long total = page.getTotal();
        List<User> list = page.getRecords();
        return R.ok().data("total",total).data("items",list);
    }

    @PostMapping("addMusic")
    @ApiOperation("添加音乐")
    public R addUser(@RequestBody(required = false) Music music){
        boolean save = musicService.save(music);
        if(save){
            return R.ok().message("保存成功");
        }else{
            return R.error().message("保存失败");
        }
    }

    @PostMapping("modifyById")
    @ApiOperation("根据id修改")
    public R modifyById(@RequestBody(required = false) Music music){
        /*根据id经行修改, 但是传入的是user对象*/
        boolean save = musicService.updateById(music);
        System.out.println(music);
        if(save){
            return R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }

    @GetMapping("selectById/{id}")
    @ApiOperation("根据id查询")
    public R selectById(@PathVariable Integer id){
        Music music = musicService.getById(id);
        return R.ok().data("music",music);
    }
}
